package com.example.myapplication

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import android.view.View
import android.webkit.*
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.Permission
import com.yzq.zxinglibrary.android.CaptureActivity
import com.yzq.zxinglibrary.bean.ZxingConfig
import com.yzq.zxinglibrary.common.Constant

import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONObject

class MainActivity : AppCompatActivity() {
    private val REQUEST_CODE_SCAN = 111
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val webSettings = webView.settings
        webSettings.domStorageEnabled = true//主要是这句
        webSettings.javaScriptEnabled = true//启用js
        webSettings.blockNetworkImage = false//解决图片不显示
        webSettings.javaScriptCanOpenWindowsAutomatically = true
        webSettings.loadsImagesAutomatically = true
        webSettings.setAppCacheEnabled(false)
        WebView.setWebContentsDebuggingEnabled(true);
        webView.settings.cacheMode = WebSettings.LOAD_NO_CACHE
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webView.settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
        }
        webView.setWebChromeClient(WebChromeClient())
        //该方法解决的问题是打开浏览器不调用系统浏览器，直接用webview打开
        webView.webViewClient =  WebViewClient()
        webView.loadUrl("http://192.168.3.18:8080/#/")
        webView.addJavascriptInterface(this, "android")
    }

    @JavascriptInterface
    fun scan() {
        AndPermission.with(applicationContext)
                .permission(Permission.CAMERA, Permission.READ_EXTERNAL_STORAGE)
                .onGranted {
                    val intent = Intent(this@MainActivity, CaptureActivity::class.java)
                    /*ZxingConfig是配置类
                                             *可以设置是否显示底部布局，闪光灯，相册，
                                             * 是否播放提示音  震动
                                             * 设置扫描框颜色等
                                             * 也可以不传这个参数
                                             * */
                    val config = ZxingConfig()
                    config.isPlayBeep = true //是否播放扫描声音 默认为true
                    config.isShake = true //是否震动  默认为true
                    config.isDecodeBarCode = true //是否扫描条形码 默认为true
                    config.reactColor = R.color.colorAccent //设置扫描框四个角的颜色 默认为白色
                    config.frameLineColor = R.color.colorAccent //设置扫描框边框颜色 默认无色
                    config.scanLineColor = R.color.colorAccent //设置扫描线的颜色 默认白色
                    config.isFullScreenScan = false //是否全屏扫描  默认为true  设为false则只会在扫描框中扫描
                    intent.putExtra(Constant.INTENT_ZXING_CONFIG, config)
                    startActivityForResult(intent, REQUEST_CODE_SCAN)
                }
                .onDenied {
                    val packageURI = Uri.parse("package:$packageName")
                    val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI)
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    startActivity(intent)
                    Toast.makeText(this@MainActivity, "没有权限无法扫描呦", Toast.LENGTH_LONG).show()
                }.start()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        // 扫描二维码/条码回传
        if (requestCode == REQUEST_CODE_SCAN && resultCode == Activity.RESULT_OK) {
            if (data != null) {
                val content = data.getStringExtra(Constant.CODED_CONTENT)
                var json=JSONObject()
                json.put("content",content)
                webView.evaluateJavascript("javascript:getScanResult(" + json + ")", null);
            }
        }
    }

}